
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/ui_self_service/mine/page_reabam_cloud_privacy.dart';
import 'package:reabampos/src/reabam/ui_self_service/pagestate_ss_home_index.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_login_utils.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
登录页
 */
class StatePageSelfServiceLogin extends XPageState{

  StatePageSelfServiceLogin();
  bool isRememberPWD=false;
  bool isReadXieyi=false;

  String? loginName;
  String? loginPwd;

  @override
  void onCreate() {

    isRememberPWD=XSharePreferencesUtils.getBool(RBConfigs.SP_isRememberPWD);
    isReadXieyi=XSharePreferencesUtils.getBool(RBConfigs.SP_isReadXieyi);

    loginName=XSharePreferencesUtils.getString(RBConfigs.SP_loginName);

    if(isRememberPWD){
      loginPwd=XSharePreferencesUtils.getString(RBConfigs.SP_loginPwd);
    }

  }

  @override
  void onStart() {
  }

  @override
  Widget getBodyWidget(BuildContext context) {
    XTempData.statusBarHeight=XFlutterUtils.getStatusBarHeight(context);
    XLogUtils.printLog('---statusBarHeight=${XTempData.statusBarHeight}');
    return XWidgetUtils.getLayoutSingleChildScrollView(child: _getUIWidget(context));
  }

  @override
  Future<void> updateData({int pageIndex=1}) async {
  }

  Widget _getUIWidget(BuildContext context){
    return XWidgetUtils.getLayoutPadding(
        padding: XWidgetUtils.getEdgeInsets(0, 206, 0, 0),
        child: XWidgetUtils.getLayoutColumn(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              XImageUtils.getImageWidgetWithAsset('reabam_logo_3.png',width: 150,height: 172),
              XWidgetUtils.getLayoutContainer(
                  padding: XWidgetUtils.getEdgeInsets(90, 155, 90, 0),
                  child: XWidgetUtils.getWidgetTextField(
                      labelText:'用户名',
                      initialValue: loginName,
                      labelTextStyle: XStyleUtils.getTextStyle(
                          fontSize: 34,
                          color: XColorUtils.color999999
                      ),
                      keyboardType: TextInputType.phone,
                      style: XStyleUtils.getTextStyle(
                          fontSize: 34,
                          color: XColorUtils.color333333
                      ),
                      onChanged: (res){
                        loginName=res;
                      }
                  )
              ),
              XWidgetUtils.getWidgetDivider(marginLeft: 90,marginRight: 90),
              XWidgetUtils.getLayoutContainer(
                  padding: XWidgetUtils.getEdgeInsets(90, 0, 90, 0),
                  child: XWidgetUtils.getWidgetTextField(
                      labelText:'密码',
                      labelTextStyle: XStyleUtils.getTextStyle(
                          fontSize: 34,
                          color: XColorUtils.color999999
                      ),
                      initialValue: loginPwd,
                      style: XStyleUtils.getTextStyle(
                          fontSize: 34,
                          color: XColorUtils.color333333
                      ),
                      isShowInputText: false,
                      onChanged: (res){
                        loginPwd=res;
                      }
                  )
              ),
              XWidgetUtils.getWidgetDivider(marginLeft: 90,marginRight: 90),
              XWidgetUtils.getLayoutContainer(
                  padding: XWidgetUtils.getEdgeInsets(75, 40, 0, 0),
                  width: double.infinity,
                  child: XWidgetUtils.getWidgetCheckbox(
                    isCurrentSelect: isRememberPWD,
                    selectColor:RBColors.reabamColor4,
                    onChanged: (bool? value) {
                      setState(() {
                        isRememberPWD = value!;
                        XSharePreferencesUtils.saveBool(RBConfigs.SP_isRememberPWD,isRememberPWD);
                      });
                    },
                    text: '记住我',
                    style: XStyleUtils.getTextStyle(
                        fontSize: 34,
                        color: XColorUtils.color999999,
                      lineHeight: 1
                    ),
                  )
              ),
              XWidgetUtils.getLayoutContainer(
                  padding: XWidgetUtils.getEdgeInsets(75, 10, 0, 0),
                  child: XWidgetUtils.getLayoutAlign(
                      child: XWidgetUtils.getLayoutRow(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            XWidgetUtils.getWidgetCheckbox(
                                isCurrentSelect: isReadXieyi,
                                selectColor:RBColors.reabamColor4,
                                onChanged: (bool? value) {
                                  setState(() {
                                    isReadXieyi = value!;
                                    XSharePreferencesUtils.saveBool(RBConfigs.SP_isReadXieyi,isReadXieyi);
                                  });
                                },
                                text: '我已阅读并同意',
                                style: XStyleUtils.getTextStyle(
                                    fontSize: 34,
                                    color: XColorUtils.color999999
                                ),
                            ),
                            XWidgetUtils.getWidgetGestureDetector(
                                child: XWidgetUtils.getWidgetText(
                                  padding: XWidgetUtils.getEdgeInsets(5, 0, 0, 0),
                                  '服务协议',
                                  style: XStyleUtils.getTextStyle(
                                      fontSize: 34,
                                      color: RBColors.reabamColor4
                                  ),
                                ),
                                onTap: (){
                                  startPage(PageStateReabamCloudPrivacy('服务协议'));
                                }
                            ),
                            XWidgetUtils.getLayoutPadding(
                                padding: XWidgetUtils.getEdgeInsets(5, 0, 5, 0),
                                child: XWidgetUtils.getWidgetText('和',style: XStyleUtils.getTextStyle(
                                    fontSize: 34,
                                    color: XColorUtils.color999999
                                ))
                            ),
                            XWidgetUtils.getWidgetGestureDetector(
                                child: XWidgetUtils.getWidgetText(
                                  '隐私协议',
                                  style: XStyleUtils.getTextStyle(
                                      fontSize: 34,
                                      color: RBColors.reabamColor4
                                  ),
                                ),
                                onTap: (){
                                  startPage(PageStateReabamCloudPrivacy('隐私协议'));
                                }
                            ),
                          ])
                  )
              ),
              RBWidgets_SelfService.getButton(
                text: '登录',
                margin: XWidgetUtils.getEdgeInsets(90, 180, 90, 0),
                onPressed: (){
                  XFlutterUtils.hideKeyboard();

                  if (XStringUtils.isEmpty(loginName)) {
                    toast("请输入用户名称");
                    return;
                  }
                  if (XStringUtils.isEmpty(loginPwd)) {
                    toast("请输入密码");
                    return;
                  }

                  if (!isReadXieyi) {

                    XFlutterUtils.openAlertDialogDefault(
                        context: context,
                        title: '提示',
                        contentStr: '请勾选阅读并同意《服务协议》和《隐私协议》！',
                        okButtonText: '已阅读并同意',
                        onCallbackResult: (String? callback){
                          if(callback!=null){
                            if('已阅读并同意'==callback){
                              setState(() {
                                isReadXieyi=true;
                                XSharePreferencesUtils.saveBool(RBConfigs.SP_isReadXieyi,isReadXieyi);
                              });
                            }
                          }
                        }
                    );

                    return;
                  }

                  //登录
                  RBLoginUtils.login(
                    this,
                    loginName: loginName??'',
                    loginPWD: loginPwd??'',
                    onCallback: (result) {
                      // 跳转各个端口的首页
                      startPage(StatePageSelfServiceHomeIndex(),isFinish: true);
                    },
                  );
                },
              ),
            ]
        )
    );
  }

}
